<?php

$page = "sites";

require 'require.php';

if (isset($_GET['s']) && ($_GET['s'])) {
	$start = $_GET['s'];
} else {
	$start = 0;
}

$link_server = mysql_connect(OPENIDOO_DB_SERVER, OPENIDOO_DB_USER, OPENIDOO_DB_PASSWORD);
$select_db = mysql_select_db(OPENIDOO_DB_DATABASE, $link_server);

if (isset($_GET['id']) && ($_GET['id']) && isset($_GET['a']) && ($_GET['a'] == 'del')) {

	if ((!$link_server) or (!$select_db)) {
		die('Impossible de se connecter');
	} else {

		if (!($verif_site = mysql_query("SELECT user_id, site_id FROM openidoo_sites WHERE site_id='".mysql_escape_string($_GET['id'])."' LIMIT 1", $link_server))) {
			die('Une erreur est survenue');
		} else {

			if (mysql_num_rows($verif_site) == 1) {
				$verif_site_user_id = mysql_result($verif_site, 0, 'user_id');
			}

			if ($verif_site_user_id == $_SESSION['user_id']) {

				if (!mysql_query("DELETE FROM openidoo_sites WHERE site_id='".mysql_escape_string($_GET['id'])."' AND user_id='".mysql_escape_string($_SESSION['user_id'])."'", $link_server)) {
					die('Une erreur est survenue');
				}

			}
		}
	}
}

if ((!$link_server) or (!$select_db)) {
	die('Impossible de se connecter');
} else {

	if (!($sql_sites = mysql_query("SELECT SQL_CALC_FOUND_ROWS site_id, user_id, date, trust_root FROM openidoo_sites WHERE user_id='".mysql_escape_string($_SESSION['user_id'])."' ORDER BY date DESC LIMIT ".mysql_escape_string($start).",20", $link_server))) {
		die('Une erreur est survenue');
	}

	if (!($sql_sites_total = mysql_query("SELECT FOUND_ROWS() as 'total'", $link_server))) {
		die('Une erreur est survenue');
	} else {
		$total_sites = mysql_result($sql_sites_total, 0, 'total');
	}
}

require "html/top.html";
require "html/sites.html";
require "html/bottom.html";

?>
